home *** CD-ROM | disk | FTP | other *** search
- package sun.net.www;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintStream;
-
- public class MessageHeader {
- private String[] keys;
- private String[] values;
- private int nkeys;
-
- public MessageHeader() {
- this.grow();
- }
-
- public MessageHeader(InputStream is) throws IOException {
- this.parseHeader(is);
- }
-
- public void add(String k, String v) {
- this.grow();
- this.keys[this.nkeys] = k;
- this.values[this.nkeys] = v;
- ++this.nkeys;
- }
-
- public static String canonicalID(String id) {
- if (id == null) {
- return "";
- } else {
- int st = 0;
- int len = id.length();
-
- boolean substr;
- char c;
- for(substr = false; st < len && ((c = id.charAt(st)) == '<' || c <= ' '); substr = true) {
- ++st;
- }
-
- while(st < len && ((c = id.charAt(len - 1)) == '>' || c <= ' ')) {
- --len;
- substr = true;
- }
-
- return substr ? id.substring(st, len) : id;
- }
- }
-
- public String findNextValue(String k, String v) {
- boolean foundV = false;
- if (k == null) {
- int i = this.nkeys;
-
- while(true) {
- --i;
- if (i < 0) {
- break;
- }
-
- if (this.keys[i] == null) {
- if (foundV) {
- return this.values[i];
- }
-
- if (this.values[i] == v) {
- foundV = true;
- }
- }
- }
- } else {
- int i = this.nkeys;
-
- while(true) {
- --i;
- if (i < 0) {
- break;
- }
-
- if (k.equalsIgnoreCase(this.keys[i])) {
- if (foundV) {
- return this.values[i];
- }
-
- if (this.values[i] == v) {
- foundV = true;
- }
- }
- }
- }
-
- return null;
- }
-
- public String findValue(String k) {
- if (k == null) {
- int i = this.nkeys;
-
- while(true) {
- --i;
- if (i < 0) {
- break;
- }
-
- if (this.keys[i] == null) {
- return this.values[i];
- }
- }
- } else {
- int i = this.nkeys;
-
- while(true) {
- --i;
- if (i < 0) {
- break;
- }
-
- if (k.equalsIgnoreCase(this.keys[i])) {
- return this.values[i];
- }
- }
- }
-
- return null;
- }
-
- public String getKey(int n) {
- return n >= 0 && n < this.nkeys ? this.keys[n] : null;
- }
-
- public String getValue(int n) {
- return n >= 0 && n < this.nkeys ? this.values[n] : null;
- }
-
- private void grow() {
- if (this.keys == null || this.nkeys >= this.keys.length) {
- String[] nk = new String[this.nkeys + 4];
- String[] nv = new String[this.nkeys + 4];
- if (this.keys != null) {
- System.arraycopy(this.keys, 0, nk, 0, this.nkeys);
- }
-
- if (this.values != null) {
- System.arraycopy(this.values, 0, nv, 0, this.nkeys);
- }
-
- this.keys = nk;
- this.values = nv;
- }
-
- }
-
- public void parseHeader(InputStream is) throws IOException {
- this.nkeys = 0;
- if (is != null) {
- char[] s = new char[10];
-
- String v;
- String k;
- for(int firstc = is.read(); firstc != 10 && firstc != 13 && firstc >= 0; this.add(k, v)) {
- int len = 0;
- int keyend = -1;
- boolean inKey = firstc > 32;
- s[len++] = (char)firstc;
-
- label106:
- while(true) {
- int c;
- if ((c = is.read()) < 0) {
- firstc = -1;
- break;
- }
-
- switch (c) {
- case 9:
- c = 32;
- case 32:
- inKey = false;
- break;
- case 10:
- case 13:
- firstc = is.read();
- if (c == 13 && firstc == 10) {
- firstc = is.read();
- if (firstc == 13) {
- firstc = is.read();
- }
- }
-
- if (firstc == 10 || firstc == 13 || firstc > 32) {
- break label106;
- }
-
- c = 32;
- break;
- case 58:
- if (inKey && len > 0) {
- keyend = len;
- }
-
- inKey = false;
- }
-
- if (len >= s.length) {
- char[] ns = new char[s.length * 2];
- System.arraycopy(s, 0, ns, 0, len);
- s = ns;
- }
-
- s[len++] = (char)c;
- }
-
- while(len > 0 && s[len - 1] <= ' ') {
- --len;
- }
-
- if (keyend <= 0) {
- k = null;
- keyend = 0;
- } else {
- k = String.copyValueOf(s, 0, keyend);
- if (keyend < len && s[keyend] == ':') {
- ++keyend;
- }
-
- while(keyend < len && s[keyend] <= ' ') {
- ++keyend;
- }
- }
-
- if (keyend >= len) {
- v = new String();
- } else {
- v = String.copyValueOf(s, keyend, len - keyend);
- }
- }
-
- }
- }
-
- public void prepend(String k, String v) {
- this.grow();
-
- for(int i = this.nkeys; i > 0; --i) {
- this.keys[i] = this.keys[i - 1];
- this.values[i] = this.values[i - 1];
- }
-
- this.keys[0] = k;
- this.values[0] = v;
- ++this.nkeys;
- }
-
- public void print(PrintStream p) {
- for(int i = 0; i < this.nkeys; ++i) {
- if (this.keys[i] != null) {
- p.print(this.keys[i] + (this.values[i] != null ? ": " + this.values[i] : "") + "\r\n");
- }
- }
-
- p.print("\r\n");
- p.flush();
- }
-
- public void set(int i, String k, String v) {
- this.grow();
- if (i >= 0) {
- if (i > this.nkeys) {
- this.add(k, v);
- } else {
- this.keys[i] = k;
- this.values[i] = v;
- }
-
- }
- }
-
- public void set(String k, String v) {
- int i = this.nkeys;
-
- do {
- --i;
- if (i < 0) {
- this.add(k, v);
- return;
- }
- } while(!k.equalsIgnoreCase(this.keys[i]));
-
- this.values[i] = v;
- }
-
- public void setIfNotSet(String k, String v) {
- if (this.findValue(k) == null) {
- this.add(k, v);
- }
-
- }
-
- public String toString() {
- String result = super.toString();
-
- for(int i = 0; i < this.keys.length; ++i) {
- result = result + "{" + this.keys[i] + ": " + this.values[i] + "}";
- }
-
- return result;
- }
- }
-